﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraCharts;
using DevExpress.Utils;
using TierService;
using OrderMgr.SysSetting;
using System.Data.SqlClient;
using TierApplication;


namespace OrderMgr.OrderInfo
{
    public partial class frmBuyInvoiceAppDetailInfo : TierService.frmTemplate
    {
        #region "Member Variables"
        public string sFuncID;
        public TierService.Utility.OperateMode OpMode;
        public string sUserID;
        public string _OrderID = "";
        public string _InvoiceID = "";
        public Utility.OrderType _OrderType;
        public Utility.OrderStatus _OrderStatus;

        T_Order mOrder = new T_Order();
        SqlDataAdapter sdaOrderDetail = new SqlDataAdapter();
        DataSet dsOrderDetail = new DataSet();

        T_OrderInvoice mOrderInvoice = new T_OrderInvoice();
        #endregion

        public frmBuyInvoiceAppDetailInfo()
        {
            InitializeComponent();
        }

        private void frmBuyAuditDetail_Load(object sender, EventArgs e)
        {
            initForm();

            switch (OpMode)
            {
                case Utility.OperateMode.Query:
                    btnSave.Visible = false;

                    showInfo(_InvoiceID);
                    break;
                case Utility.OperateMode.Insert:
                    txtInvoiceDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    txtInvoiceRcvDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    txtInvoiceUserID.EditValue = TierService.Utility.gUserID;
                    txtOrderType.EditValue = ((int)_OrderType).ToString();
                    //txtDepotID.EditValue = ((int)Utility.DepotType.Material).ToString();
                    //txtRemark.Size = new Size(742, 83);
                    //txtRemark.Location = new Point(82, 81);
                    if (_OrderID != "")
                        txtOrderID.Text = _OrderID;

                    break;
                case Utility.OperateMode.Update:
                    btnSave.Enabled = true;


                    showInfo(_InvoiceID);
                    break;
                case Utility.OperateMode.Delete:
                    break;
                default:
                    break;
            }

        }

        private void initForm()
        {
            Utility.DataToLookupComboxEx(txtOrderType, C_DMB.GetListByType(C_DMB.DType.OrderType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtDepotID, C_DMB.GetListByType(C_DMB.DType.DepotType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtOrderUserID, (new P_User()).GetList(""), "UserID", "UserName");
            Utility.DataToLookupComboxEx(txtAuditUserID, (new P_User()).GetList(""), "UserID", "UserName");
            //Utility.DataToLookupComboxEx(txtPayAuditUserID, (new P_User()).GetList(""), "UserID", "UserName");
            Utility.DataToLookupComboxEx(txtPayUserID, (new P_User()).GetList(""), "UserID", "UserName");
            Utility.DataToLookupComboxEx(txtSupplierID, (new T_Supplier()).GetList(""), "SupplierID", "SupplierName");
            Utility.DataToLookupComboxEx(txtPayType, C_DMB.GetListByType(C_DMB.DType.PayType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtPayingUserID, (new P_User()).GetList(""), "UserID", "UserName");
            Utility.DataToLookupComboxEx(txtInvoiceUserID, (new P_User()).GetList(""), "UserID", "UserName");

            txtOrderUserID.Enabled = false;
            txtOrderType.Enabled = false;

        }


        private void showInfo(string sID)
        {
            mOrderInvoice.GetModel(sID);
            this.txtInvoiceID.Text = mOrderInvoice.InvoiceID;
            this.txtOrderID.Text = mOrderInvoice.OrderID;
            if (mOrderInvoice.OrderDate != null)
                this.txtOrderDate.DateTime = mOrderInvoice.OrderDate.Value;
            this.txtOrderUserID.EditValue = mOrderInvoice.OrderUserID;
            //this.txtOrderStatus.EditValue = mOrder.OrderStatus;
            this.txtDepotID.EditValue = mOrderInvoice.DepotID;
            //this.txtToDepotID.EditValue = mOrder.ToDepotID;
            this.txtAuditUserID.EditValue = mOrderInvoice.AuditUserID;
            if (mOrderInvoice.AuditDate != null)
                this.txtAuditDate.DateTime = mOrderInvoice.AuditDate.Value;
            this.txtRemark.Text = mOrderInvoice.Remark;

            //////////---------------Invoice
            this.txtInvoiceRemark.Text = mOrderInvoice.InvoiceRemark;
            if (mOrderInvoice.InvoiceRcvDate != null)
                this.txtInvoiceRcvDate.DateTime = mOrderInvoice.InvoiceRcvDate.Value;
            this.txtInvoiceNo.Text = mOrderInvoice.InvoiceNo;
            this.txtInvoiceMoney.Text = mOrderInvoice.InvoiceMoney.Value.ToString();
            if (mOrderInvoice.InvoiceDate != null)
                this.txtInvoiceDate.DateTime = mOrderInvoice.InvoiceDate.Value;


        }
        private void GetFormReturValue(string str)
        {
            string sID = str;
            if (sID != "")
            {
                showInfo(sID);
            }
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            //Close();
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //if (txtSaleInvoiceMoney.Value - txtInvoicedMoney.Value < txtInvoiceMoney.Value)
            //{
            //    XtraMessageBox.Show("开票金额不能大于“合同总金额”-“已开票金额”!");
            //    return;
            //}
            if (SaveOrder())
            {
                XtraMessageBox.Show("开票信息保存成功!");
            }

            btnSubmitAudit.Visible = true;

            //if (txtInvoiceMoney.Value != txtPayFee.Value)
            //{
            //    if (MessageBox.Show("发票金额与采购金额不一致，是否录入？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            //        return;
            //}


            //mOrder.OrderID = this.txtOrderID.Text;
            //mOrder.OrderDate = DateTime.Parse(this.txtOrderDate.Text);
            //mOrder.OrderUserID = (string)this.txtOrderUserID.EditValue;
            //mOrder.OrderType = (string)this.txtOrderType.EditValue;
            //mOrder.DepotID = (string)this.txtDepotID.EditValue;
            //mOrder.Remark = this.txtRemark.Text;
            //mOrder.Sign = 0;
            //mOrder.Transfer = "0";

            //mOrder.InvoiceUserID = (string)this.txtInvoiceUserID.EditValue;
            //mOrder.InvoiceRcvDate = this.txtInvoiceRcvDate.DateTime;
            //mOrder.InvoiceNo = this.txtInvoiceNo.Text;
            //mOrder.InvoiceMoney = this.txtInvoiceMoney.Value;
            //mOrder.InvoiceDate = this.txtInvoiceDate.DateTime;
            //mOrder.InvoiceRemark = this.txtInvoiceRemark.Text;
            //try
            //{
            //    mOrder.InvoiceApp();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("发票登记出错,错误原因为:" + ex.Message);
            //    return;
            //}

            //MessageBox.Show("发票登记成功!");


        }


        private bool SaveOrder()
        {
            if (this.txtInvoiceDate.Text == "")
            {
                XtraMessageBox.Show("开票日期不能为空,请录入!");
                txtInvoiceDate.Focus();
                return false;
            }

            if (this.txtInvoiceRcvDate.Text == "")
            {
                XtraMessageBox.Show("收票日期不能为空,请录入!");
                txtInvoiceDate.Focus();
                return false;
            }
            mOrderInvoice.InvoiceRcvDate = this.txtInvoiceRcvDate.DateTime;
            mOrderInvoice.InvoiceID = this.txtInvoiceID.Text;
            mOrderInvoice.OrderID = this.txtOrderID.Text;
            mOrderInvoice.OrderDate = DateTime.Parse(this.txtOrderDate.Text);
            mOrderInvoice.OrderUserID = (string)this.txtOrderUserID.EditValue;
            mOrderInvoice.OrderStatus = "0";
            mOrderInvoice.DepotID = (string)this.txtDepotID.EditValue;
            if (_OrderStatus != Utility.OrderStatus.S0Input)
            {
                mOrderInvoice.AuditUserID = (string)this.txtAuditUserID.EditValue;
                mOrderInvoice.AuditDate = DateTime.Parse(this.txtAuditDate.Text);
            }
            mOrderInvoice.Remark = this.txtRemark.Text;

            mOrderInvoice.OrderSort = "0";//销售开票
            mOrderInvoice.InvoiceNo = this.txtInvoiceNo.Text;
            mOrderInvoice.InvoiceMoney = this.txtInvoiceMoney.Value;
            mOrderInvoice.InvoiceDate = this.txtInvoiceDate.DateTime;
            mOrderInvoice.InvoiceRemark = this.txtInvoiceRemark.Text;

            if ((mOrderInvoice.OrderID == ""))
            {
                XtraMessageBox.Show("请选择销售订单编号!");
                txtOrderID.Focus();
                return false;
            }
            bool flag = false;
            switch (OpMode)
            {
                case TierService.Utility.OperateMode.Insert:
                    try
                    {
                        mOrderInvoice.InvoiceID = Utility.GetSeqval(Utility.SeqName.InvoiceID.ToString());
                        mOrderInvoice.Add();
                        _InvoiceID = mOrderInvoice.InvoiceID;

                        txtInvoiceID.Text = _InvoiceID;
                        OpMode = Utility.OperateMode.Update;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("开票信息新增出错,错误原因为:" + ex.Message);
                        break;
                    }
                    //MessageBox.Show("入库单新增成功!");
                    btnSubmitAudit.Enabled = true;
                    flag = true;
                    //DialogResult = DialogResult.OK;
                    break;
                case TierService.Utility.OperateMode.Update:
                    try
                    {
                        // mCustomer.CustID = txtCustID.Text;
                        mOrderInvoice.Update();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("开票信息保存出错,错误原因为:" + ex.Message);
                        break;
                    }


                    btnSubmitAudit.Enabled = true;
                    //MessageBox.Show("入库单信息修改成功!");
                    //DialogResult = DialogResult.OK;
                    flag = true;
                    break;
                default:
                    break;
            }
            return flag;
        }

        private void btnAudit_Click(object sender, EventArgs e)
        {

            //if (MessageBox.Show("是否要对此单据进行付款", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            //    return;
            //string sOrderID = txtOrderID.Text;

            //T_Order mOrder = new T_Order();
            //mOrder.Payment(sOrderID, Utility.gUserID);
            //MessageBox.Show("付款成功");
            //DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnDeAudit_Click(object sender, EventArgs e)
        {

            //if (MessageBox.Show("是否要退回此单据?", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            //    return;


            //string sOrderID = txtOrderID.Text;

            //T_Order mOrder = new T_Order();
            //mOrder.DeAuditPayApp(sOrderID, Utility.gUserID, txtPayReason.Text);
            //MessageBox.Show("审核退回成功");
            //DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnSubmitAudit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否要将此单据提交审核吗？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;
            string sOrderID = txtInvoiceID.Text;
            T_OrderInvoice mOrderInvoice = new T_OrderInvoice();
            mOrderInvoice.SubmitAudit(sOrderID);
            MessageBox.Show("单据提交审核成功");
            Close();
        }

        private void txtOrderID_EditValueChanged(object sender, EventArgs e)
        {
            mOrder.GetModel(txtOrderID.Text);
            this.txtOrderID.Text = mOrder.OrderID;
            if (mOrder.OrderDate != null)
                this.txtOrderDate.DateTime = mOrder.OrderDate.Value;
            this.txtOrderUserID.EditValue = mOrder.OrderUserID;
            this.txtOrderType.EditValue = mOrder.OrderType;
            this.txtDepotID.EditValue = mOrder.DepotID;
            this.txtAuditUserID.EditValue = mOrder.AuditUserID;
            if (mOrder.AuditDate != null)
                this.txtAuditDate.DateTime = mOrder.AuditDate.Value;
            this.txtRemark.Text = mOrder.Remark;
            this.txtSupplierID.EditValue = mOrder.SupplierID;

            this.txtContractNo.Text = mOrder.ContractNo;
            this.txtPayType.EditValue = mOrder.PayType.ToString();
            if (mOrder.PayRstDate != null)
                this.txtPayRstDate.DateTime = mOrder.PayRstDate.Value;
            this.txtPayerFullName.Text = mOrder.PayerFullName;
            this.txtPayerBank.Text = mOrder.PayerBank;
            this.txtPayeBankAccount.Text = mOrder.PayeBankAccount;
            this.txtPayFee.Text = mOrder.PayFee.ToString();
            this.txtPayUserID.EditValue = mOrder.PayUserID;
            if (mOrder.PayingDate != null)
                this.txtPayingDate.DateTime = mOrder.PayingDate.Value;
            this.txtPayingUserID.EditValue = mOrder.PayingUserID;

            ////////////---------------Invoice
            //if (mOrder.InvoiceRcvDate != null)
            //    this.txtInvoiceRcvDate.DateTime = mOrder.InvoiceRcvDate.Value;
            //this.txtInvoiceRemark.Text = mOrder.InvoiceRemark;
            //this.txtInvoiceUserID.EditValue = (string)mOrder.InvoiceUserID;
            //this.txtInvoiceNo.Text = mOrder.InvoiceNo;
            //this.txtInvoiceMoney.Text = mOrder.InvoiceMoney.Value.ToString();
            //if (mOrder.InvoiceDate != null)
            //    this.txtInvoiceDate.DateTime = mOrder.InvoiceDate.Value;

            //if (mOrder.InvoiceAuditRemark != "")
            //{
            //    txtDeAuditReson.Visible = true;
            //    txtDeAuditReson.Text = "审核退回原因：" + mOrder.InvoiceAuditRemark;
            //}
        }

    }
}